************************************************************************************
**  Replication code for "Fiscal Incentives for Conflict"
**  Notes: Mine panel analysis of illegal mining proxies (Tables 1, 3, A19-A21; Figure 2)
*************************************************************************************

set more off

global mines = "F:/Dropbox (ESOC - Princeton)/Public services and conflict in India/Mining"


* Users can create separate folders for graphs and table output, the standard setting is to save all output in the main directory.
global mytables "$mines/Restat_Repository"
global myfigures "$mines/Restat_Repository"

cd "$mines/Restat_Repository"

u "FINAL_Replication_Mines", clear


*--------------------------------
* Table 1, Panels D and E
*-------------------------------

estimates clear

estpost summarize excess_dum_vill excess_dum50_vill expired_base_vill truck_active 
estout using "$mytables/Table1_PanelD.tex", label cells("count(fmt(0)) mean(fmt(2)) sd(fmt(2))") mlabels(none) collabels(none) style(tex) replace
estimates clear

estpost summarize iron_dum bauxite_dum chromite_dum manganese_dum if period==2008
estout using "$mytables/Table1_PanelE.tex", label cells("count(fmt(0)) mean(fmt(2)) sd(fmt(2))") mlabels(none) collabels(none) style(tex) replace
estimates clear

*--------------------------------
* TABLE 3; and TABLE A20 Panel B
*-------------------------------


gen est_sample=1
global main "(freq==1)"
global balanced "(counter==7)|(counter==6)"

foreach sample in "main"  "balanced"  {
preserve

if "`sample'"=="balanced" {
local suff="_full"
}

eststo: xtreg excess_dum_vill`suff' treat_post1 treat_post2 i.state_id##i.year   if $`sample', fe cluster(panelid)
		estadd ysumm

eststo: xtreg excess_dum50_vill`suff' treat_post1 treat_post2 i.state_id##i.year   if $`sample', fe cluster(panelid)
		estadd ysumm

eststo: xtreg truck_active`suff' treat_post1 treat_post2 treat_post1_exp_vill treat_post2_exp_vill i.state_id##i.year  i.expired_base_vill##i.year  if $`sample', fe cluster(panelid)
		estadd ysumm

if "`sample'"=="main" {
esttab using "$mytables/Table3.tex", keep(treat_post1 treat_post1_exp_vill treat_post2 treat_post2_exp_vill) stats(ymean N N_clust, labels("Mean DV"  "Observations" "Clusters") fmt(3 0 0))  fragment nomtitles nonumbers  nowrap booktabs  nogaps  nolines eqlabels("") style(tex) wide label  replace starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par)) substitute((.) "" 0.000 "")
replace est_sample=e(sample)
estimate clear
}

else if "`sample'"=="balanced" {
esttab using "$mytables/TableA21_PanelB.tex", keep(treat_post1 treat_post1_exp_vill treat_post2 treat_post2_exp_vill) stats(ymean ysd N N_clust, labels("Mean DV" "Standard Deviation" "Observations" "Clusters") fmt(3 3 0 0))  fragment nomtitles nonumbers  nowrap booktabs  nogaps  nolines eqlabels("") style(tex) wide label  replace starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par)) substitute((.) "" 0.000 "")
replace est_sample=e(sample)
estimate clear
}

restore
estimates clear

}

*----------------------------------------------------
* TABLE A18: illegal mining -imputed measurements
*---------------------------------------------------

xi: eststo: reg imputed iron_dum i.state_id,  cluster(panelid)
estadd ysumm
esttab using "$mytables/TableA19.tex", keep(iron_dum) stats(ymean ysd N N_clust, labels("Mean DV" "Standard Deviation" "Observations" "Clusters") fmt(3 3 0 0))  fragment nomtitles nonumbers  nowrap booktabs  nogaps  nolines eqlabels("") style(tex) wide label  replace starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par)) substitute((.) "" 0.000 "")

estimates clear


*---------------------------------------------------------------------------
* TABLE A19: illegal mining - imputed measurements difference-in-difference
*---------------------------------------------------------------------------


eststo: xtreg imputed treat_post1 treat_post2 i.state_id##i.year , fe cluster(panelid)
		estadd ysumm

eststo: xtreg imputed treat_post1 treat_post2 treat_post1_exp_vill treat_post2_exp_vill i.state_id##i.year , fe  cluster(panelid)
		estadd ysumm

esttab using "$mytables/TableA20.tex", keep(treat_post1 treat_post1_exp_vill treat_post2 treat_post2_exp_vill) stats(ymean ysd N N_clust, labels("Mean DV" "Standard Deviation" "Observations" "Clusters") fmt(3 3 0 0))  fragment nomtitles nonumbers  nowrap booktabs  nogaps  nolines eqlabels("") style(tex) wide label  replace starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par)) substitute((.) "" 0.000 "")

estimates clear 



*---------------------------------------------------------------------------
* Table A20, Panel A: Illegal Mining - balanced samples
*---------------------------------------------------------------------------
* See Table 3 above for the code of Table A20 Panel B

foreach sample in  "all" {
preserve
sort panelid year
keep if inrange(period,2007,2011)
collapse (last) truck_active (last) excess_dum_vill excess_dum50_vill expired_base_vill treat_post treat_post1_exp_vill year, by(panelid post state_id)
bys panelid: egen collapsecount=total(excess_dum_vill!=.)
keep if collapsecount==2
label var treat_post "Iron Mine x Post"
label var treat_post1_exp_vill "Iron Mine x Post x Expired"

eststo: xtreg excess_dum_vill treat_post i.post i.state_id##i.post , fe cluster(panelid)
		estadd ysumm
eststo: xtreg excess_dum50_vill treat_post i.post i.state_id##i.post , fe cluster(panelid)
		estadd ysumm
restore
preserve
sort panelid year
keep if inrange(period,2007,2011)
collapse (last) truck_active (last) excess_dum_vill excess_dum50_vill expired_base_vill treat_post treat_post1_exp_vill year, by(panelid post state_id)
bys panelid: egen collapsecount=total(excess_dum_vill!=.)
keep if collapsecount==2
label var treat_post "Iron Mine x Post"
label var treat_post1_exp_vill "Iron Mine x Post x Expired"
eststo: xtreg truck_active treat_post treat_post1_exp_vill i.post i.state_id##i.post i.expired_base_vill##i.post, fe cluster(panelid)
		estadd ysumm
esttab using "$mytables/TableA21_PanelA.tex", keep(treat_post treat_post1_exp_vill) stats(ymean ysd N N_clust, labels("Mean DV" "Standard deviation" "Observations" "Clusters") fmt(3 3 0 0))  fragment nomtitles nonumbers  nowrap booktabs  nogaps  nolines eqlabels("") style(tex) wide label  replace starlevels(* 0.10 ** 0.05 *** 0.01) collabels(none)  cells(b(star fmt(%9.3f)) se(par)) substitute((.) "" 0.000 "")
estimates clear
restore

}

* Additional code (not reported in paper), including 2012-2013 in the second period
foreach sample in  "all" {
preserve
sort panelid year
keep if inrange(period,2007,2013)
collapse (last) truck_active (last) excess_dum_vill excess_dum50_vill expired_base_vill treat_post treat_post2_exp_vill year, by(panelid post state_id)
label var treat_post "Iron Mine x Post"
label var treat_post2_exp_vill "Iron Mine x Post x Expired"
eststo: xtreg excess_dum_vill treat_post i.post i.state_id##i.post , fe cluster(panelid)
		estadd ysumm
eststo: xtreg excess_dum50_vill treat_post i.post i.state_id##i.post , fe cluster(panelid)
		estadd ysumm
restore
preserve
sort panelid year
keep if inrange(period,2007,2013)
collapse (last) truck_active (last) excess_dum_vill excess_dum50_vill expired_base_vill treat_post treat_post2_exp_vill year, by(panelid post state_id)
eststo: xtreg truck_active treat_post treat_post2_exp_vill i.post i.state_id##i.post i.expired_base_vill##i.post, fe cluster(panelid)
		estadd ysumm
estimates clear
restore
}

*---------------------------
* FIGURE 2: Illegal Mining
*--------------------------

forvalues y=2007(1)2013 {
gen iron_`y'=(iron_dum)*(period==`y')
gen ironexp_`y'=(iron_dum)*expired_base_vill*(period==`y')
}


* PANEL A
**********

preserve
matrix hrid = J(24 ,8, 0)
foreach outcome in  "excess_dum"   {
foreach mineral in "iron"   {
quie reghdfe `outcome'_vill iron_2007 o.iron_2008 iron_2009 iron_2010-iron_2013 ,  a(panelid year state_id##year) cluster(panelid)
quie lincom (iron_2007+iron_2008)/2
local mean1 = `r(estimate)'
local sd1 = `r(estimate)'
quie lincom (iron_2009+iron_2010+iron_2011)/3
local mean2 = `r(estimate)'
local sd2 = `r(estimate)'
quie lincom (iron_2012+iron_2013)/2
local mean3 = `r(estimate)'
local sd3 = `r(estimate)'
global iter  = 1
forvalues i=2007(1)2013 {
  quie lincom  iron_`i'
  matrix hrid[$iter,3] = `r(estimate)'
  matrix hrid[$iter,2] = `r(estimate)' - 1.98 * `r(se)'
  matrix hrid[$iter,4] = `r(estimate)'+ 1.98* `r(se)'
  local ttt=`i'-2009
  matrix hrid[$iter, 1] = `ttt'
  matrix hrid[$iter, 5] = 1
   matrix hrid[$iter, 7] = `mean1'
  matrix hrid[$iter, 6] = `mean1'-1.98*`sd1'
  matrix hrid[$iter, 8] = `mean1'+1.98*`sd1'
	if `i'>=2009 {
	matrix hrid[$iter, 7] = `mean2'
	matrix hrid[$iter, 6] = `mean2'-1.98*`sd2'
	matrix hrid[$iter, 8] = `mean2'+1.98*`sd2'
	}
	if `i'>=2012 {
	matrix hrid[$iter, 7] = `mean3'
	matrix hrid[$iter, 6] = `mean3'-1.98*`sd3'
	matrix hrid[$iter, 8] = `mean3'+1.98*`sd3'
	}
	
	global iter=$iter+1

  }

svmat hrid
sort hrid1
quie su hrid2
local miny = `r(min)'
quie su hrid4
local maxy = `r(max)'
drop if hrid2==0 & hrid3==0 & hrid1!=-1
quie su hrid1
sort hrid1
twoway (connected hrid3 hrid1,ytitle("Coefficient estimate", axis(1))) (rcap hrid2 hrid4 hrid1 , lcolor(gs6) lwidth(vthin) xline(-0.5,lcolor(gs6)))  (line hrid7 hrid1 if hrid1<0, col(gs6) lp(dash) lwidth(thin))  (line hrid7 hrid1 if hrid1>=0 & hrid1<3 , col(gs6) lp(dash) lwidth(thin))  (line hrid7 hrid1 if hrid1>=3 , col(gs6) lp(dash) lwidth(thin)), scheme(s1mono) legend(off) xtitle("Years to treatment", size(4)) yline(0, lp(dot) lwidth(vthin))
graph export "$myfigures/Figure2_PanelA.png", replace
drop hrid*

}
}
restore



* PANEL B
**********


preserve
matrix hrid = J(24 ,8, 0)
foreach outcome in  "truck_active"  {
foreach mineral in "iron"   {
display("check")
quie reghdfe `outcome' ironexp_2007 o.ironexp_2008 ironexp_2009 ironexp_2010-ironexp_2013 iron_2007 iron_2008 iron_2009 iron_2010-iron_2013 ,  a(panelid year state_id##year expired_base_vill##year) cluster(panelid)
display("check")
quie lincom (ironexp_2007+ironexp_2008)/2
local mean1 = `r(estimate)'
local sd1 = `r(estimate)'
quie lincom (ironexp_2009+ironexp_2010+ironexp_2011)/3
local mean2 = `r(estimate)'
local sd2 = `r(estimate)'
quie lincom (ironexp_2012+ironexp_2013)/2
local mean3 = `r(estimate)'
local sd3 = `r(estimate)'
global iter  = 1
forvalues i=2007(1)2013 {
  quie lincom  ironexp_`i'
  matrix hrid[$iter,3] = `r(estimate)'
  matrix hrid[$iter,2] = `r(estimate)' - 1.98 * `r(se)'
  matrix hrid[$iter,4] = `r(estimate)'+ 1.98* `r(se)'
  local ttt=`i'-2009
  matrix hrid[$iter, 1] = `ttt'
  matrix hrid[$iter, 5] = 1
  matrix hrid[$iter, 7] = `mean1'
  matrix hrid[$iter, 6] = `mean1'-1.98*`sd1'
  matrix hrid[$iter, 8] = `mean1'+1.98*`sd1'
	if `i'>=2009 {
	matrix hrid[$iter, 7] = `mean2'
	matrix hrid[$iter, 6] = `mean2'-1.98*`sd2'
	matrix hrid[$iter, 8] = `mean2'+1.98*`sd2'
	}
	if `i'>=2012 {
	matrix hrid[$iter, 7] = `mean3'
	matrix hrid[$iter, 6] = `mean3'-1.98*`sd3'
	matrix hrid[$iter, 8] = `mean3'+1.98*`sd3'
	}
		global iter=$iter+1

  }
svmat hrid
sort hrid1
quie su hrid2
local miny = `r(min)'
quie su hrid4
local maxy = `r(max)'
drop if hrid2==0 & hrid3==0 & hrid1!=-1
quie su hrid1
sort hrid1
twoway (connected hrid3 hrid1,ytitle("Coefficient estimate", axis(1))) (rcap hrid2 hrid4 hrid1 , lcolor(gs6) lwidth(vthin) xline(-0.5,lcolor(gs6)))  (line hrid7 hrid1 if hrid1<0, col(gs6) lp(dash) lwidth(thin))  (line hrid7 hrid1 if hrid1>=0 & hrid1<3 , col(gs6) lp(dash) lwidth(thin))  (line hrid7 hrid1 if hrid1>=3 , col(gs6) lp(dash) lwidth(thin)), scheme(s1mono) legend(off) xtitle("Years to treatment", size(4)) yline(0, lp(dot) lwidth(vthin))
graph export "$myfigures/Figure2_PanelB.png", replace
drop hrid*

}
}
restore
